package pl.th.permission;

import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;

import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import java.util.Arrays;
import java.util.List;
import java.util.stream.LongStream;

import static pl.th.permission.PermissionCompat.PERMISSION_CREATE_NOTIFICATION;
import static pl.th.permission.PermissionCompat.buildAppDetailIntent;

public class Utils {

    public static boolean isAllPermissionsGranted(String[] permissions, Activity activity) {
        for (String permission : permissions) {
            if (ContextCompat.checkSelfPermission(activity, permission) != PackageManager.PERMISSION_GRANTED) {
                return false;
            }
        }
        return true;
    }

    public static boolean isPermissionsGranted(String permission, Activity activity) {
        return ContextCompat.checkSelfPermission(activity, permission) == PackageManager.PERMISSION_GRANTED;
    }

    public static boolean isPermissionEmpty(String[] permissions) {
        return permissions == null || permissions.length == 0;
    }

    @SuppressWarnings("BooleanMethodIsAlwaysInverted")
    public static boolean isPermissionEmpty(List<String> permissions) {
        return permissions == null || permissions.size() == 0;
    }

    static boolean isAndroidLAndUp(int sdkVersion) {
        return sdkVersion >= Build.VERSION_CODES.LOLLIPOP;
    }

    static boolean isAndroidMAndUp(int sdkVersion) {
        return sdkVersion >= Build.VERSION_CODES.M;
    }

    static boolean isAndroidOAndUp(int sdkVersion) {
        return sdkVersion >= Build.VERSION_CODES.O;
    }

    static boolean isAndroidPAndUp(int sdkVersion) {
        return sdkVersion >= Build.VERSION_CODES.P;
    }

    static boolean isAndroidQAndUp(int sdkVersion) {
        return sdkVersion >= Build.VERSION_CODES.Q;
    }

    static boolean isAndroidSAndUp(int sdkVersion) {
        return sdkVersion >= Build.VERSION_CODES.S;
    }

    static boolean isAndroidRAndUp(int sdkVersion) {
        return sdkVersion >= Build.VERSION_CODES.R;
    }

    static Uri buildPackageUri(Context context) {
        return Uri.fromParts("package", context.getPackageName(), null);
    }

    @SuppressWarnings("unused")
    public static boolean contentEquals(String[] a1, String[] a2) {
        if (a1 == null && a2 == null) return true;
        if (a1 == null || a2 == null) return false;
        if (a1.length != a2.length) return false;
        Arrays.sort(a1);
        Arrays.sort(a2);
        return Arrays.equals(a1, a2);
    }

}
